Styling the Masked Text Box

The MaskedTextBox control allows you to define separate styles for user input, mask literals and prompts (positions where user input is allowed but there is not currently any user input). In addition, you can customize the display of prompts based on the type of input required, for example so the user can see whether they have to type a letter or number.

For examples, see MaskedTextBoxStyling.xaml in the Elements sample.

Styling

To customize the display of input, literal and prompt characters, use the InputStyle, LiteralStyle and PromptStyle properties of the MaskedTextBox. The TargetType of each style should be Inline.

Customizing the Prompt Character

To customize what is displayed in a prompt position, use the PromptCharDisplaySelector property of the MaskedTextBox.

You will need to implement the IPromptCharDisplaySelector interface. Your implementation receives an IPrompt indicating the expected input type and whether the position is mandatory or optional. You should return an Inline representing the desired display. This should be a Run containing a single character, or an InlineUIContainer containing a single UIElement. (Runs containing multiple characters will cause an error.)

Returning a Run instead of just a char enables you to specify fonts, font weights, colors, etc. instead of just a single prompt character.
 

Returning a UIElement allows you to create your own graphics and potentially even to use animations, tooltips, etc. However, be careful to keep your prompts small—remember you want users to think of them as placeholders for a single character!
 

Note that if PromptStyle is set, this style is applied to all prompts, even if the default prompt has been overridden using PromptCharDisplaySelector.

Templating

You can create custom templates for the MaskedTextBox using the standard WPF approach. Any custom template you create must contain the following elements:

PART_RichTextBox The MaskedTextBox displays output and receives input through this part. It must be a RichTextBox (or a type derived from RichTextBox). If this part is missing, or is not a RichTextBox, the MaskedTextBox will not be able to display output or receive input.